const Service = require('../../../core/service/ApiService');

class RecordDao extends Service {
    //借用记录
    async getRecord(user_id) {
        const sql = `select user_record.device_id,record_id,device_pic,device_name,position,device_number,belong,user_record.ctime,device.status
                    from user_record
                    inner join device on user_record.device_id = device.device_id
                    where user_record.user_id = ${user_id}
                    order by user_record.ctime ASC`;
        return this.model.query(sql, {type: this.model.QueryTypes.SELECT});
    }

    //借用排行
    async getRecordRunk() {
        //根据user_id从user表中查询出wx_id，统计user_id的数量并分组，按照count的大小排序
        const sql = ` select count(A.user_id) as count,user.wx_name,user.wx_pic,user.wx_id,@rownum:=@rownum+1 as rownum
                            from ((user_record A),(SELECT @rownum:=0) B)
                            inner join user on user.user_id = A.user_id
                            group by A.user_id
                            order by count DESC;`;
        return this.model.query(sql, {type: this.model.QueryTypes.SELECT});

    }

    //个人记录排名
    async personal(open) {
        const sql = ` select * from (
                             select count(A.user_id) as count,A.user_id,user.open_id,user.wx_name,user.wx_pic,user.wx_id,@rownum:=@rownum+1 as rownum
                            from ((user_record A),(SELECT @rownum:=0) B)
                            inner join user on user.user_id = A.user_id
                            group by A.user_id
                            order by count DESC
                            ) AS C
                             where C.open_id='${open}'`;
        return this.model.query(sql, {type: this.model.QueryTypes.SELECT});
    }
    //设备借出
    async addRecord(info) {
        return this.model.UserRecord.create({
            device_id:info.device_id,
            user_id:info.user_id,
            ctime:info.nowDate,
            status:info.status,
            delay_status:this.constant.DELAY_STATUS.UNDELAY
        });
    }

    //检查记录是否存在
    async getRecordInfo(device_id,user_id) {
        return this.model.UserRecord.findOne({
            where:{
                device_id:device_id,
                user_id:user_id,
                status:this.constant.EMPLOY_STATUS.SIGNALED
            },
            attributes: ['record_id'],
            raw:true
        })
    }

    //提交
    async commit(info) {
        return this.model.UserRecord.update(info,{
            where: {
                device_id: info.device_id,
                user_id:info.user_id
            }
        })
    }

    async updateDeviceStatus(u_info) {
        return this.model.Device.update({
            status:this.constant.DEVICE_STATUS.ENABLE
        },{
            where:{
                device_id:u_info.device_id
            }
        })
    }

    async getRecordInfoFromDb(record_id) {
        return this.model.UserRecord.findOne({
            where:{
                record_id:record_id,
                status:this.constant.EMPLOY_STATUS.SIGNALED,
                delay_status: this.constant.DELAY_STATUS.UNDELAY
            },
            attributes: ['device_id','user_id'],
            raw:true
        })
    }
    async delay(delay_info) {
        return this.model.UserRecord.update({
            ctime:delay_info.date,
            delay_status:this.constant.DELAY_STATUS.HAVE_DELAY
        },{
            where:{
                record_id:delay_info.record_id,
                device_id:delay_info.device_id,
                user_id:delay_info.user_id,
                delay_status:this.constant.DELAY_STATUS.UNDELAY
            }
        })
    }

}

module.exports = RecordDao;
